﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ThumpThumpBus.Models.Domain;
using ThumpThumpBus.Models.Repository;
using ThumpThumpBus.Models.ViewModels;

namespace ThumpThumpBus.Controllers.Api
{
    public class IncidentController : ApiController
    {
        private readonly IncidentRepository _incidentRepository = new IncidentRepository();

        public IEnumerable<Incident> Get()
        {
            var incidents = _incidentRepository.GetAllIncidents();
            return incidents;
        }

        public Incident Get(string id)
        {
            var incident = _incidentRepository.GetAllIncidents().FirstOrDefault(x => x.id.ToString() == id);
            return incident;
        }

        public HttpResponseMessage Post(Incident incident)
        {
            _incidentRepository.AddIncident(incident);

            incident.id =  _incidentRepository.GetAllIncidents().Max(x => x.id) + 1 ;

            var response = Request.CreateResponse(HttpStatusCode.Created, incident);

            var uri = Url.Link("DefaultApi", new { id = incident.id});
            response.Headers.Location = new Uri(uri);
            return response;
        }

        public HttpResponseMessage Put(Incident incident)
        {
            _incidentRepository.AddIncident(incident);

            incident.id = _incidentRepository.GetAllIncidents().Max(x => x.id) + 1;

            var response = Request.CreateResponse(HttpStatusCode.NoContent);

            var uri = Url.Link("DefaultApi", new { id = incident.id });
            response.Headers.Location = new Uri(uri);
            return response;
        }

    }
}
